/*
 * CItemEditor.java
 * 
 * Copyright (c) 2009 Giles Millward (Manigra). All rights reserved.
 * 
 * This file is part of JRogueDPS.
 * 
 * JRogueDPS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * JRogueDPS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with JRogueDPS.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * CItemEditor.java
 *
 * Created on 16-Apr-2009, 16:21:01
 */

package geardatabase;

import javax.swing.JFormattedTextField;
import utility.CMessage;

/**
 *
 * @author Giles Millward (Manigra)
 */
public class CItemEditor extends javax.swing.JDialog {

    boolean m_result = false;
    CItem m_item;
    
    /** Creates new form CItemEditor */
    public CItemEditor(java.awt.Frame parent, CItem p_itemId) {
        super(parent, p_itemId.getName(), true);
        initComponents();
        this.setLocationRelativeTo(parent);
        m_item = p_itemId;
        this.setExistingValues();
    }

    private void setExistingValues()
    {
        // Populate the fields with the existing item stats data
        this.strengthFormattedTextField.setText("" + this.m_item.getItemStats().getStrength());
        this.agilityFormattedTextField.setText("" + this.m_item.getItemStats().getAgility());
        this.hitRatingFormattedTextField.setText("" + this.m_item.getItemStats().getHitRating());
        this.critRatingFormattedTextField.setText("" + this.m_item.getItemStats().getCritRating());
        this.attackPowerFormattedTextField.setText("" + this.m_item.getItemStats().getAttackPower());
        this.hasteRatingFormattedTextField.setText("" + this.m_item.getItemStats().getHasteRating());
        this.expertiseRatingFormattedTextField.setText("" + this.m_item.getItemStats().getExpertiseRating());
        this.armorPenRatingFormattedTextField.setText("" + this.m_item.getItemStats().getArmorPenRating());
    }

    public boolean getValue()
    {
        this.setVisible(true);
        return m_result;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        hasteRatingFormattedTextField = new javax.swing.JFormattedTextField();
        attackPowerFormattedTextField = new javax.swing.JFormattedTextField();
        agilityFormattedTextField = new javax.swing.JFormattedTextField();
        expertiseRatingFormattedTextField = new javax.swing.JFormattedTextField();
        strengthFormattedTextField = new javax.swing.JFormattedTextField();
        hitRatingFormattedTextField = new javax.swing.JFormattedTextField();
        armorPenRatingFormattedTextField = new javax.swing.JFormattedTextField();
        critRatingFormattedTextField = new javax.swing.JFormattedTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        okayButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jLabel1.setText("Edit the stats for the item here and click on Okay to commit those stats");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Stats"));

        hasteRatingFormattedTextField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));

        attackPowerFormattedTextField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));

        agilityFormattedTextField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));

        expertiseRatingFormattedTextField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));

        strengthFormattedTextField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));

        hitRatingFormattedTextField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));

        armorPenRatingFormattedTextField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));

        critRatingFormattedTextField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));

        jLabel2.setText("Strength");

        jLabel3.setText("Agility");

        jLabel4.setText("Attack Power");

        jLabel5.setText("Haste Rating");

        jLabel6.setText("Hit Rating");

        jLabel7.setText("Critical Strike Rating");

        jLabel8.setText("Armor Pen Rating");

        jLabel9.setText("Expertise Rating");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 45, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(armorPenRatingFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(critRatingFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(hitRatingFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(hasteRatingFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(attackPowerFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(expertiseRatingFormattedTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE)
                    .addComponent(strengthFormattedTextField)
                    .addComponent(agilityFormattedTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(strengthFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(agilityFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(attackPowerFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(hasteRatingFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(hitRatingFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(critRatingFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(armorPenRatingFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(expertiseRatingFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        okayButton.setText("Okay");
        okayButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okayButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 355, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(112, 112, 112)
                        .addComponent(okayButton, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(cancelButton)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(okayButton)
                    .addComponent(cancelButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        m_result = false;
        dispose();
    }//GEN-LAST:event_formWindowClosing

    private void okayButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okayButtonActionPerformed
        if (this.strengthFormattedTextField.getText().trim().equals(""))
        {
            CMessage.showError("Strength field must have a value!");
            return;
        }

        if (this.agilityFormattedTextField.getText().trim().equals(""))
        {
            CMessage.showError("Agility field must have a value!");
            return;
        }

        if (this.hitRatingFormattedTextField.getText().trim().equals(""))
        {
            CMessage.showError("Hit Rating field must have a value!");
            return;
        }

        if (this.critRatingFormattedTextField.getText().trim().equals(""))
        {
            CMessage.showError("Critical Strike Rating field must have a value!");
            return;
        }

        if (this.attackPowerFormattedTextField.getText().trim().equals(""))
        {
            CMessage.showError("Attack Power field must have a value!");
            return;
        }

        if (this.hasteRatingFormattedTextField.getText().trim().equals(""))
        {
            CMessage.showError("Haste Rating field must have a value!");
            return;
        }

        if (this.expertiseRatingFormattedTextField.getText().trim().equals(""))
        {
            CMessage.showError("Expertise Rating field must have a value!");
            return;
        }

        if (this.armorPenRatingFormattedTextField.getText().trim().equals(""))
        {
            CMessage.showError("Armor Pen Rating field must have a value!");
            return;
        }


        this.m_item.getItemStats().setStrength(getFieldValue(strengthFormattedTextField));
        this.m_item.getItemStats().setAgility(getFieldValue(agilityFormattedTextField));
        this.m_item.getItemStats().setHitRating(getFieldValue(hitRatingFormattedTextField));
        this.m_item.getItemStats().setCritRating(getFieldValue(critRatingFormattedTextField));
        this.m_item.getItemStats().setAttackPower(getFieldValue(attackPowerFormattedTextField));
        this.m_item.getItemStats().setHasteRating(getFieldValue(hasteRatingFormattedTextField));
        this.m_item.getItemStats().setExpertiseRating(getFieldValue(expertiseRatingFormattedTextField));
        this.m_item.getItemStats().setArmorPenRating(getFieldValue(armorPenRatingFormattedTextField));
        this.m_item.setModified(true);
        m_result = true;
        dispose();
    }//GEN-LAST:event_okayButtonActionPerformed

    private int getFieldValue(JFormattedTextField p_field)
    {
        return Integer.parseInt(p_field.getText().trim());
    }

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        m_result = false;
        dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JFormattedTextField agilityFormattedTextField;
    private javax.swing.JFormattedTextField armorPenRatingFormattedTextField;
    private javax.swing.JFormattedTextField attackPowerFormattedTextField;
    private javax.swing.JButton cancelButton;
    private javax.swing.JFormattedTextField critRatingFormattedTextField;
    private javax.swing.JFormattedTextField expertiseRatingFormattedTextField;
    private javax.swing.JFormattedTextField hasteRatingFormattedTextField;
    private javax.swing.JFormattedTextField hitRatingFormattedTextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton okayButton;
    private javax.swing.JFormattedTextField strengthFormattedTextField;
    // End of variables declaration//GEN-END:variables

}
